<template>
  <div>
    <el-card class="box-card">
      <!-- 搜索区域 -->
      <div class="search-box">
        <el-input
          v-model="searchRecruitmentId"
          placeholder="请输入招聘记录ID"
          style="width: 200px"
          class="filter-item"
        ></el-input>
        <el-button type="primary" @click="searchByRecruitmentId">查询</el-button>
        <el-button type="success" @click="handleAdd">添加面试记录</el-button>
      </div>

      <!-- 表格区域 -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="index" label="序号" width="80"></el-table-column>
        <el-table-column prop="recruitmentId" label="招聘记录ID" width="120"></el-table-column>
        <el-table-column prop="stage" label="面试阶段" width="120"></el-table-column>
        <el-table-column prop="score" label="面试得分" width="120"></el-table-column>
        <el-table-column prop="evaluation" label="评价"></el-table-column>
        <el-table-column label="操作" width="200">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 添加/编辑对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="40%">
      <el-form :model="form" :rules="rules" ref="form" label-width="100px">
        <el-form-item label="招聘记录ID" prop="recruitmentId" v-if="!isEdit">
          <el-input v-model="form.recruitmentId"></el-input>
        </el-form-item>
        <el-form-item label="面试阶段" prop="stage">
          <el-select v-model="form.stage" placeholder="请选择面试阶段">
            <el-option label="初试" value="初试"></el-option>
            <el-option label="复试" value="复试"></el-option>
            <el-option label="终试" value="终试"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="面试得分" prop="score">
          <el-input-number
            v-model="form.score"
            :min="0"
            :max="100"
            :precision="2"
            :step="5"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="面试评价" prop="evaluation">
          <el-input
            type="textarea"
            v-model="form.evaluation"
            :rows="4"
            placeholder="请输入面试评价"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm('form')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      searchRecruitmentId: "",
      dialogVisible: false,
      dialogTitle: "添加面试记录",
      isEdit: false,
      form: {
        recruitmentId: "",
        stage: "初试",
        score: 80,
        evaluation: ""
      },
      rules: {
        recruitmentId: [
          { required: true, message: "请输入招聘记录ID", trigger: "blur" },
          { type: 'number', message: '招聘记录ID必须为数字', transform: (value) => Number(value) }
        ],
        stage: [
          { required: true, message: "请选择面试阶段", trigger: "change" }
        ],
        score: [
          { required: true, message: "请输入面试得分", trigger: "blur" },
          { type: 'number', message: '面试得分必须为数字', trigger: 'blur' }
        ],
        evaluation: [
          { required: true, message: "请输入面试评价", trigger: "blur" }
        ]
      },
      currentRecordId: null
    };
  },
  created() {
    this.getAllRecords();
  },
  methods: {
    // 获取所有面试记录
    getAllRecords() {
      this.$http.get("/api/interview_records")
        .then(response => {
          if (response.data.code === 200) {
            this.tableData = response.data.data;
          } else {
            this.$message.error(response.data.msg);
          }
        });
    },

    // 按招聘记录ID搜索
    searchByRecruitmentId() {
      if (!this.searchRecruitmentId) {
        this.getAllRecords();
        return;
      }
      this.$http.get(`/api/interview_records/${this.searchRecruitmentId}`)
        .then(response => {
          if (response.data.code === 200) {
            this.tableData = response.data.data;
          } else {
            this.$message.error(response.data.msg);
          }
        });
    },

    // 打开添加对话框
    handleAdd() {
      this.isEdit = false;
      this.dialogTitle = "添加面试记录";
      this.form = {
        recruitmentId: "",
        stage: "初试",
        score: 80,
        evaluation: ""
      };
      this.dialogVisible = true;
    },

    // 打开编辑对话框
    handleEdit(row) {
      this.isEdit = true;
      this.dialogTitle = "编辑面试记录";
      this.currentRecordId = row.recordId;
      this.form = { ...row };
      this.dialogVisible = true;
    },

    // 提交表单
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.isEdit) {
            // 更新记录
            this.$http.put(`/api/interview_records/${this.currentRecordId}`, JSON.stringify(this.form), {
              headers: {
                'Content-Type': 'application/json'
              }
            }).then(response => {
                if (response.data.code === 200) {
                  this.$message.success("更新成功");
                  this.dialogVisible = false;
                  this.getAllRecords();
                } else {
                  this.$message.error(response.data.msg);
                }
              });
          } else {
            // 添加记录
            this.$http.post("/api/interview_records", JSON.stringify(this.form), {
              headers: {
                'Content-Type': 'application/json'
              }
            }).then(response => {
                if (response.data.code === 200) {
                  this.$message.success("添加成功");
                  this.dialogVisible = false;
                  this.getAllRecords();
                } else {
                  this.$message.error(response.data.msg);
                }
              });
          }
        }
      });
    },

    // 删除记录
    handleDelete(row) {
      this.$confirm('确认删除该面试记录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http.delete(`/api/interview_records/${row.recordId}`)
          .then(response => {
            if (response.data.code === 200) {
              this.$message.success("删除成功");
              this.getAllRecords();
            } else {
              this.$message.error(response.data.msg);
            }
          });
      }).catch(() => {});
    }
  }
};
</script>

<style scoped>
.search-box {
  margin-bottom: 20px;
}
.filter-item {
  margin-right: 10px;
}
</style>
